// Version 1.00
function isValidDate(day, month, year) {
  day = day - 0;
  month = month - 0;
  year = year - 0;
  
  month_length = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
  
  if ( (year%4) != 0 )
    month_length[2] = 28;
  else if ( (year%400) == 0 )
    month_length[2] = 29;
  else if ( (year%100) == 0 )
    month_length[2] = 28;
  else                       
    month_length[2] = 29;     
    
  if ( month < 1 || month > 12 )
    return false;  
  
  else if ( day < 1 || day > month_length[month] )
    return false;  
    
  return true;      
}

// Version 1.00
function isBlank(s)
{
  for(var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

// Version 1.00
function isNumeric(s) {
  pattern=/^\d*$/;
  if (pattern.test(s))
    return true;
  else
    return false;
}

// Version 1.00
function isEmail(s)
{   
  // there must be >= 1 character before @, so we
  // start looking at character position 1 
  // (i.e. second character)
	    
  var i = 1;
  var sLength = s.length;

  // look for @
  while ((i < sLength) && (s.charAt(i) != "@"))
  { i++ }
  if ((i >= sLength) || (s.charAt(i) != "@")) return false;
  else i += 2;

  // look for .
  while ((i < sLength) && (s.charAt(i) != "."))
  { i++ }
  // there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
  else return true;
}

// Version 1.00
function isDecimal(s) {
  pattern=/^\d*(.)\d*$/;
  if (pattern.test(s))
    return true;
  else
    return false;
}

// Version 1.00
function isMailChar(s) {
  //pattern=/^[A-Za-z0-9@._-#&]*$/;
  pattern=/^[\w@._-]*$/;
  if (pattern.test(s))
    return true;
  else
    return false;
}

// Version 1.00
function isLoginChar(s) {
  //pattern=/^[A-Za-z0-9@._-#&]*$/;
  pattern=/^[\w._-]*$/;
  if (pattern.test(s))
    return true;
  else
    return false;
}

// Version 1.00
function isAlphabet(s) {
  //pattern=/^[A-Za-z0-9@._-#&]*$/;
  pattern=/^[A-Za-z]*$/;
  if (pattern.test(s))
    return true;
  else
    return false;
}

// Version 1.00
function isDocumentFile(s) {
  pattern=/^[\w\W]*.pdf|.doc$/;
  if (pattern.test(s.toLowerCase()))
    return true;
  else
    return false;
}

// Version 1.00
function isImageFile(s) {
  pattern=/^[\w\W]*.jpg|.jpeg|.gif$/;
  if (pattern.test(s.toLowerCase())) {
    return true;
  }
  else
    return false;
}

// Version 1.00
function isZipFile(s) {
  pattern=/^[\w\W]*.zip$/;
  if (pattern.test(s.toLowerCase())) {
    return true;
  }
  else
    return false;
}

// Version 1.00
function isCSVFile(s) {
  pattern=/^[\w\W]*.csv$/;
  if (pattern.test(s.toLowerCase()))
    return true;
  else
    return false;
}

// Version 1.00
// Date 2010-04-18
// Remarks from OhoCode.com
function isCitizenID(s) {
  for(i=0, sum=0; i < 12; i++)
    sum += parseFloat(s.charAt(i)) * (13-i);
  if ((11-sum%11)%10 != parseFloat(s.charAt(12))) return false;
  return true;
}

// Version 1.00
// check value of Textbox object If is null return true
function chkTxbNull(obj, message)
{
  var s = obj.value
  if (isBlank(s) || s == null)
  {
    alert(message);
	  obj.focus();             
    return true;
  }
  else
    return false;
}

// Version 1.00
// check value of Textbox object If is not numeric return true
function chkTxbNotNumeric(obj, message)
{
  var s = obj.value
  if (!isNumeric(s))
  {
    alert(message);
	obj.focus();             
	obj.select();
    return true;
  }
  else
    return false;
}

// Version 1.00
// check value of Textbox object If is not numeric return true
function chkTxbNotDecimal(obj, message)
{
  var s = obj.value
  if (!isDecimal(s))
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;
}


// Version 1.00
// Date 2010-04-18
// check value of Textbox object If is not CitizenID return false
function chkTxbNotCitizenID(obj, message)
{
  var s = obj.value
  if (!isCitizenID(s))
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;
}

// Version 1.00
// check value of Textbox object If is not numeric return true
function chkTxbNotEmail(obj, message)
{
  var s = obj.value
  if (!isEmail(s))
  {
    alert(message);
	  obj.focus();             
    return true;
  }
  else
    return false;
}

// Version 1.00
// check value of Textbox object If is not valid Qty return true
function chkTxbNotQty(obj, message)
{
  var s = obj.value
  if (!isNumeric(s) || s == "0")
  {
    alert(message);
	  obj.focus();             
    return true;
  }
  else
    return false;
}

// Version 1.00
// check length of Textbox object If length is over return true
function chkTxbNotAlphabet(obj, message)
{
  var s = obj.value
  if (!isAlphabet(s))
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;
}

// Version 1.00
// check length of Textbox object If length is over return true
function chkTxbLen(obj, len, message)
{
  var s = obj.value
  if (s.length > len)
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;
}

// Version 1.00
// check length of Textbox object If length is equal return true
function chkTxbSize(obj, len, message)
{
  var s = obj.value
  if (s.length != len)
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;
}

// Version 1.00
// check radio is selected if selected then return true
function chkRadioNull(obj, message)
{
  if (isNaN(obj.length)) {
    if (obj.checked)
      return false;
    else {
      alert(message);
      obj.focus();
      return true;
    }      
  }
  else {
    for (var i=0; i < obj.length; i++) {
      if (obj[i].checked) {
        return false;
        break;
      }
    }
    alert(message);
    obj[0].focus();
    return true;
  }
}

// Version 1.00
function chkDocumentFile(obj, message)
{
  var s = obj.value
  if (!isDocumentFile(s))
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;
}

// Version 1.00
function chkImageFile(obj, message)
{
  var s = obj.value
  if (!isImageFile(s))
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;  
}

// Version 1.00
function chkZipFile(obj, message)
{
  var s = obj.value
  if (!isZipFile(s))
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;  
}

// Version 1.00
function chkCSVFile(obj, message)
{
  var s = obj.value
  if (!isCSVFile(s))
  {
    alert(message);
  	obj.focus();             
    return true;
  }
  else
    return false;
}

function validate_numeric(obj) {
  if (obj.value != "")
    if (chkTxbNotNumeric(obj, "กรุณากรอก ค่าตัวเลข ให้ถูกต้อง!")) return;
}
 
function validate_decimal(obj) {
  if (obj.value != "")
    if (chkTxbNotDecimal(obj, "กรุณากรอก ค่าจำนวนเงิน ให้ถูกต้อง!")) return;
}

function validate_email(obj) {
  if (obj.value != "")
    if (chkTxbNotEmail(obj, "กรุณากรอก ค่าอีเมล์ ให้ถูกต้อง!")) return;
}

function validate_citizen_id(obj) {
  if (obj.value != "")
    if (chkTxbNotCitizenID(obj, "กรุณากรอก เลขที่บัตรประชาชน ให้ถูกต้อง!")) return;
}

function validate_length(obj, maxlength) {
  if (obj.value != "") {
    if (obj.value.length > maxlength) {
      alert("กรุณากรอกข้อมูลไม่เกิน " + maxlength + " ตัวอักษร!");
      obj.focus();
      return;
    }
  }
}

function enable_controls(frm, enable, control_list) {
  var arr = control_list.split("|");
  for (i=0; i < arr.length; i++) {
    frm.elements[arr[i]].disabled = !enable;
  }
  if (enable) frm.elements[arr[0]].focus();
}

/*
function enable_controls_option(frm, control_value, enable_value, control_list) {
  //var enable;
  if (control_value == enable_value) enable = true; else enable = false;
  var arr = control_list.split("|");
  for (i=0; i < arr.length; i++) {
    frm.elements[arr[i]].disabled = !enable;
  }
}
*/